<?php
/* MHT.INC.PHP by martin $ 2007/05/07 22:30:01
*/

checkADMIN();

function html2mht($yyyymm){
 global $back, $_SMILIES, $DIVISION;
 $log = LOG."$yyyymm".EXT;
 if(!is_file(PATH.$log)){
  return $DIVISION['header'] .= "<h4>ファイル: $log が見当たりません。</h4>";
 }

 $new = DB.$yyyymm.'.mht';
 $LINES = file($log);
 $encode =strtolower(ENCODE);
 $title = BLOG_NAME.'::'.$yyyymm;
 $header = 'archive: '.date('Y/m', strtotime($yyyymm."01"));
 $gntime = date('D, d M Y H:i:s+09:00', time());
 $img_loc = ROOT_PATH;
 $img_dir = IMG_DIR;
 $att_dir = AFFIXED_DIR;
 $imgs = array();
 $contents_loc = ROOT_PATH;
 $BOUNDARY = "----=_NextPart_".$yyyymm;
 $generator = 'ppBlog '.PPBLOG_VERSION;
 
 $mht =<<<__MHT
From: <ppBlog HTML2MHT Converter>
Subject: archive: $yyyymm
Date: $gntime
MIME-Version: 1.0
Content-Type: multipart/related;
  boundary="$BOUNDARY";
  type="text/html"

This is a multi-part message in MIME format.

--$BOUNDARY
Content-Type: text/html;
  charset="$encode"
Content-Transfer-Encoding: 8bit
Content-Location: $contents_loc

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="content-type" content="text/html;charset=$encode">
<meta name="generator" content="$generator" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-script-type" content="text/javascript" />
<meta http-equiv="imagetoolbar" content="no" />
<title>$title</title>
<script type="text/javascript">/*<![CDATA[*/
var d = document;
var ie = (d.selection && !window.opera) ? 1 : 0;

function readMore(self){
 var ob = self.parentNode.childNodes[1]; // Thanks, Saru-san!
 if(ob.style.display!="block"){
  ob.parentNode.firstChild.title = '記事をたたむ';
  ob.parentNode.firstChild.innerHTML = '&laquo;やっぱり隠す';
  ob.parentNode.firstChild.style.backgroundPosition = "0 100%";
  ob.style.display = "block";
 } else if(ob.style.display=="block") {
  ob.parentNode.firstChild.title = '続きをよむ';
  ob.parentNode.firstChild.innerHTML = '&raquo;続きを読む';
  ob.parentNode.firstChild.style.backgroundPosition = "0 0";
  ob.style.display = "none";
 }
}

function loadMedia(ob, movf, ftype){ /* from ppBlog v1.5.5 */
 var clsid;
 if(ob.href.match(/\.3gp|x\-mpeg/)){
  clsid = d.all ? 'classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"' :
               'type="video/3gpp" data="'+movf+'"';
 } else clsid = 'type="'+ftype+'" data="'+movf+'"';
 ob.style.display = "none";
 ob.parentNode.parentNode.innerHTML += '<div class="media-part">'
  + '<p title="動画へのダイレクトリンク">::<a class="media-link" href="'+movf+'">'+movf+'</a>::</p>'
  + '<object '+clsid+' width="176" height="160">'
  + '<param name="type" value="'+ftype+'" />'
  + '<param name="src" value="'+movf+'" />'
  + '<param name="autoplay" value="true" />'
  + '</object></div>';
}
toggle = function (ob){
 var tar = ob.parentNode.nextSibling;
 tar.style.display = tar.style.display=="none" ? "block" : "none";
 ob.value = ob.value=="コメント表示" ? "コメント非表示" : "コメント表示";
}
/*]]>*/</script>
<style type="text/css">
* { margin: 0; padding: 0; }
html,body { margin : 20px auto 30px 20px; font-size: 100% !important; font-family: Arial, Verdana, Meiryo, MS P Gothic, sans-serif; background: #fff; }
a, a:link, a:visited, a:active { text-decoration: none; }
a:link, a:visited { color: #6a74a6; }
a:hover, a:active { color: navy; text-decoration: underline; }
a img { border: none; }
strong { color: #7b4d4b; }
em { border-bottom: 3px double #ccc; font-style: normal; }
kbd { font: 500 12px verdana, arial, sans-serif; text-decoration: underline; }
acronym, abbr { cursor: help; }
h3,h4,h5,h6 { font-weight: normal; }
h1, h2, h3, h4, h5 { margin: 15px 0; text-align: center; }
h1 { font: 800 35px Times New Roman, serif;  margin: 30px auto; text-align :left; }
h2 { font-size: 190%; }
h3 { font-size: 130%; }
h4 { font-size: 130%; }
h5 { font-size: 100%; }
label { cursor: pointer; }
address { width :100%; text-align: center; margin: 2em 0 1em 0; font: 500 15px Times New Roman; }
/*------------------[ 汎用クラス ]--------------------------*/
.hidden { display: none !important; }
.jump { padding-right: 1em; cursor: pointer; background: url(Images/jump.png) no-repeat right top;
  display: block; width: 50px; float: right;
}
.center, .alert { width: 99%; text-align: center; margin: 30px auto; }
hr.clear { clear:both; height: 1px; padding-bottom: 1px; margin-bottom: -1px; border: none; width: 100%; visibility: hidden; }
.separator { clear: both; border: none; text-align: center; margin-top: 0px; background: url(Images/divider.png) no-repeat 50% top; }
ul.navigation-list { margin: 2em auto; width: 600px; list-style-type: square; font-size: 93%; line-height: 1.5; }
ul.navigation-list li { padding-left: 3em; list-style-type: square;}
/* --------------------[ 記事ボックススタイル ]----------------------*/
.article { font-size: 96%; line-height: 1.2; margin: 15px auto; padding: 1px; width: 720px;}
.article-united { margin: 0 2em; }
.article-category { font-size: 12px; padding-left: 2em; }
.article-date { font: 800 20px georgia, arial, serif; color: #a6afc2; padding: 2px 10px 2px 0;
  text-align: right; margin-top: -45px; margin-bottom: 10px; }
.article-title { font: 800 18px arial, sans-serif; letter-spacing : 1px; padding : 4px 0 2px 18px;
  text-align: left; border-left: solid 15px navy; width: 80%; }
* html .article-title { height: 1%; } /* for buggy IE */
div.article .article-title a, div.article .article-content a { text-decoration: none; color: #5e729e; }
div.article .article-content a { border-bottom: dotted 1px #86b933; }
div.article .article-title a:hover,
div.article .article-content a:hover { text-decoration: underline; border-bottom: 0; }
.article-content { line-height : 1.4; letter-spacing : 1px; padding: 1em 1em .5em 1em; }
* html .articel-content {height: 100%;}
* html .article-content br { /* for buggy IE */ line-height: 0; }
.article-content ul, .article-content dl { /* 記事内リスト */ margin: 1em 3em; }
.article-content ul li, .article-content dl dt{ /* 記事内リスト */ padding-left: 15px; }
.article-content ol { /* 記事内orderedリスト */ list-style-type: decimal; }
.article span.readmore { cursor: pointer; color: navy; padding-left: 1em; }
.article .article-bottom { text-align : right; padding-right: 1em; background : #fbfbf9;
  border-bottom: 2px solid #efefff; clear: both; }
.article-united .article .article-bottom { border: none; }
object { width: 93%; margin: auto; overflow: auto; }
.draft { /*--[ ドラフト記事囲み用 ]--*/  border: dashed 1pt crimson; height: 100%; }
.draft:before, .draft-before { content: "未公開の記事です"; color: crimson; font-size: 11px; padding: .3em 1em; }
.media-part { width: 200px; margin: auto; text-align: center; }
/*------------------------[ 引用フォーマット ]-------------*/
div.quote { border-left : 2px solid #ddd; border-right : 2px solid #ddd; background-color : #fff;
  font-size : 95%; padding : 10px; margin: 1em auto; width: 90%; color : #00468c; clear: both;}
blockquote { font-size : 95%; padding : 10px; width: 90%; margin: 1em auto; color : #00468c; background: #f0efec; clear: both;}
blockquote dl { padding: 3px 10px 0px 10px; }
q { font-size : 12px; margin: 1em; color : #00468c; }
pre { font: 500 15px/1.4 courier new,Verdana, monospace; background : #f7f7f7; border : double 4px #808080;
  border-width: 0 0 0 4px; margin: 1em auto; padding: 20px; width: 89%; min-height: 4em;
  height: auto; color: navy; clear: both; overflow-x: auto; letter-spacing: 0.1px; }
code { font: 500 15px/1.4 courier new,Verdana, monospace; color: #2d542c; letter-spacing: 0.1px; }
ul.decimal { list-style-type: decimal; padding-left: 1em; background: #fff; font-size: 90%; margin: 2em; }
ul.decimal li { background-image: none; }
.underline { text-decoration: underline; }
.google-it { font : 600 13px 'Times New Roman',serif; color : #2020ff; cursor : pointer; }
dl dt { font-weight: 600; }
dl dd { padding: 0 2em 0 1em; margin-bottom: 0.8em; line-height: 145%; color: #444; }
dd a { text-decoration: underline; }
.attached { clear: both; color: #003264; margin-top: 20px; }
p.nextpage { color: crimson; font-size: 90%; padding-left: 1em;}
/* ------------------------[ コメント表示用 ]--------------------*/
.comment-div { text-align : left; font-size : 95%; margin: 0 auto; width: 540px; }
.comment-div .comment-header { text-align: center; font-size: 120%; color: navy; }
.comment-div a{ text-decoration: underline; }
.comment-div .comment-entry p { color: #555; text-align: left; }
.comment-div a.No{ font: 600 20px Georgia, Trebuchet MS, Arial, sans-serif; text-decoration: none;}
.comment-div .comment-entry { margin:0 auto auto 2em; }
.comment-div .non-disclosure { border: 1px dotted #aaa; }
.comment-div .non-disclosure:before { content: "[非公開]"; font-size: 80%; color: crimson; }
.comment-div .comment-index { margin-top: 15px; }
.comment-div .comment-index-owner { margin: -5px auto 0 1em; }
.comment-div .comment-entry-owner { padding-left: 4em; }
.comment-div .comment-body{ margin: 10px 0 0 1em; line-height: 1.3; padding-left: 1em; }
.comment-quote { color: #7e7a94; border-left: 2px #ddd solid; padding: 10px; }
.comment-owner { width: 18px; height: 18px; padding-left: 5px; }
hr.comment-separator { border-width: 0; visibility: hidden; margin: 0 0 0 4em; }
.comment-private { color: crimson; font-size: 80%; }
/*------------------------[ 写真・ドロップシャドウ設定 ]-----------------*/
p.photo-noshadowp, p.photo-shadow { padding: 3px; float: left; margin: 15px 15px 3px 15px; }
p.photo-shadow img, p.photo-noshadow img { display: block; padding: 7px;
  border: 1px solid #ccc; border-color: #aaa #888 #999 #bbb;
  background-color: #fff; margin: 0px 7px 8px 0px; }
.photo-shadow img.borderless, p.photo-noshadow img.borderless{ padding: 0; }
.photo-caption { margin: 0px 10px 20px 27px; text-align: left; font-size: 13px; color: navy; clear: both; }
.al-left { float: left; padding: 1px; /* Moz bug ? */ margin-right: 1em; }
.al-right { float: right; padding: 1px; /* Moz bug ? */ }
.al-center { margin: auto;}
</style>
</head>
<body>
<h1>$header</h1>

__MHT;
 $body = '<p style="padding: 10px 0 0 10em;">'.date('Y年m月', strtotime($yyyymm."01")).' は '.count($LINES).' 件の書き庫があります。</p>'.NL;
 $body .= '<ul class="navigation-list">'.NL;
 $CMTS_NUM = $PING_NUM = array();
 foreach ($LINES as $j=>$line){
  list($id,$cat,$title,) = explode('|', $line);
  $CMTS_NUM[$j] = '('.get_cmt_array($id, TRUE).')';
  $PING_NUM[$j] = '('.get_tb_count($id).')';
  $body .= '<li><a href="#UID'.$id.'">'.$title.'</a>&mdash; '.$cat.' <span class="georgia"> '
        .date('Y-m-d', str_replace('d', '', $id)).' &nbsp;'.$CMTS_NUM[$j].' '.$PING_NUM[$j].'</span></li>'.NL;
 }
 $body .= '</ul>'.NL;

 foreach ($LINES as $j=>$line){
  list($id,$cat,$title,$com,$author,$allowComment,$ping,$excerpt,$extended) = explode('|', $line);
  $cmts_array = array();
  if(!empty($extended)){
   $_extended = explode(',', $extended);
   foreach ($_extended as $i=>$_ext){
    $com .= '<!--END--><hr class="clear" /><p class="nextpage">[ここから'.($i+2).'ページ目]</p>'.$_ext;
   }
  }
  $com = preg_replace('{\[file:([^:]+?):([^/]*?)/\]}', "<a href=\"".ROOT_PATH."index.php?mode=download&amp;aim=$1\">[ファイル:$1]</a>", $com);
  $com = str_replace(array_keys($_SMILIES), '', $com);
  //_box($id,$cat,$title,$com,$author=',',$prev_same=0,$next_same=0,$cmtNum=0,$tbNum=0,$cat_icon='',$alone=0,$index=0)
  $com = NL._box($id, $cat, $title, $com.'<!--END-->',ONAME,0,0,array($CMTS_NUM[$j], 0),array($PING_NUM[$j], 0),'',1,0,1)."\n";
  if($allowComment){
   $cmts_array = get_cmt_array($id);
   if(!empty($cmts_array)){
    $parts = explode('|', $cmts_array[0]);
    $cmt_count = ADMIN ? $parts[3] : (int)($parts[3] - $parts[4]);
   } else $cmt_count = 0;
  } else $cmt_count = -1; # コメント受付なし

  $_com = NL;
  if($cmt_count >= 1) $_com .= '<p class="center"><button type="button" onclick="toggle(this);return false;">コメント表示</button></p><div class="comment-div" style="display: none;">'.NL.'<p class="comment-header">この記事に対するコメント['.$cmt_count.'件]</p>'.NL;
  
  foreach ($cmts_array as $i => $c){
   $c_id = $c_name = $c_color = $c_com = $c_ip = $c_url = $c_mail = $c_nondisc = '';
   if($i==0) continue;
   $p = explode('|', $c);
   $c_id = $p[0]; $c_name = $p[1]; $c_color = $p[2]; $c_com = $p[3];
   $c_ip = isset($p[4]) ? $p[4] : ''; $c_url = isset($p[5]) ? $p[5] : '';
   $c_mail = !empty($p[6]) ? '<a href="mailto:'.my_decrypt($p[6]).'">[mail]</a>' : '';
   if($p[7]==1){
    if(!ADMIN) continue;
    else $c_nondisc = ' non-disclosure';
   }
   $extlink = (ENABLE_NEW_WINDOW) ? ' onclick="window.open(this.href,\'\');return false;"' : '';
   $url = preg_match('{'.V_URL.'}', $c_url) ? ' <a href="'.$c_url.'"'.$extlink.'>[url]</a> ' : '';
   $c_com = preg_replace('{\[q\]`?(.*?)`?\[/q\]`?}is', NL.'<p class="comment-quote">\\1</p>'.NL, $c_com);
   $c_com = str_replace('`', '<br />', $c_com);
   if($c_name==ONAME || isRegisteredUser($c_name)){
    $_com .= ' <div class="comment-entry-owner">
  <p class="comment-index-owner"><span class="owner" id="CID'.$c_id.'"> &raquo; '.$c_name.$url.' &nbsp;'.date('Y/m/d h:i A', $c_id).'</span></p>
 <div class="comment-body" style="color:#'.$c_color.';">'.$c_com.'</div>
</div>
';
   } else {
    $_com .= ' <div class="comment-entry'.$c_nondisc.'">
 <p class="comment-index"><a class="No" id="CID'.$c_id.'" title="Permalink" href="index.php?UID='.$id.'#CID'.$c_id.'">'.$i.'.</a>&nbsp; '.$c_name.$url.$c_mail.' &mdash; '.date('Y/m/d h:i A', $c_id).' &nbsp;IP: '.$c_ip.'</p>
 <div class="comment-body" style="color:#'.$c_color.';">'.$c_com.'</div>
</div><!--#comment-entry-->
';
   }
   $_com .= '<hr class="comment-separator" />'.NL;
  } # foreach
  if($cmt_count >= 1) $_com .= '</div><!--#comment-div-->'.NL;
  
  if(strpos($com, '<!--HIDE-->')){
   $com = preg_replace('{<!\-\-HIDE\-\->(.+?)(?:<!\-\-/HIDE\-\->|(<!\-\-END\-\->))}s', NL.'<div class="readmore-div"><span class="readmore" title="もっと読む" onclick=\'readMore(this);return false;\'>もっと読む&raquo;</span><div style="display:none">$1</div>'.NL.'$2</div>', $com);
  }

  $com = my_parser($com, ENABLE_BR);
  $com = preg_replace('{<p class="article\-posted">.*?</p>}s', '<!--{p.article-posted}-->', $com);
  $com = preg_replace('{<!\-\-.+<rdf:RDF.*?</rdf:RDF>.+?\-\->}s', '<!--RDF-->', $com);
  
  if(preg_match_all('{<img(?: class="borderless" | )src="([^"]*?)".+?/>}i', $com, $mt)){ // 画像を含むとき
   foreach ($mt[1] as $i=>$imgf){
    if(is_file($imgf)){
     $size = getImageSize($imgf);
     $imgs[$img_loc.$imgf] = $size['mime'];
     $com = preg_replace('{<img src="(.+?)".+?width="([0-9]+?)" height="([0-9]+?)" alt="(.*?)".+? />}', '<img src="\\1" width="\\2" height="\\3" alt="\\4" />', $com);
    }
   }
  }
  if(preg_match_all('{<a href="media/}i', $com, $mt)){ // 動画を含むとき
   foreach ($mt[0] as $i=>$mov_dir){
    $com = str_replace("<a href=\"$mov_dir/","<a href=\"{$img_loc}$mov_dir/", $com);
   }
  }
  $body .= ($com.$_com);
 }
 $body .= '<address>ppBlog::html2mht generator</address>'.NL;
 $mht .= ($body."</body>\n"."</html>\n\n");
 
 $fp = fopen($new, 'ab');
 fwrite($fp, $mht);
 fclose($fp);

 if(!empty($imgs)){
  foreach ($imgs as $imgf=>$mime){
   $img = str_replace($img_loc,"",$imgf);
   $enc_img = chunk_split(base64_encode(get_file_content($img)));
   $mht =<<<__MHT
--$BOUNDARY
Content-Type: $mime
Content-Transfer-Encoding: base64
Content-Location: $imgf

$enc_img

--$BOUNDARY
 
__MHT;
   $fp = fopen($new, 'ab');
   fwrite($fp, $mht);
   fclose($fp);
  }
 }
 header("Content-Type: application/octet-stream; name=\"".basename($new)."\"");
 header("Content-Disposition: attachment; filename=\"".basename($new)."\"");
 readfile(PATH.$new);
 unlink(PATH.$new);
 update_MHT($yyyymm, time(), true);
 exit;
}

?>